package pri.damai.xiaowu.anydoor.business;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
import pri.damai.xiaowu.anydoor.netty.HandlerHolder;

import java.io.IOException;

/**
 *
 *
 * @description
 * @author YangXinFu
 * @date 16:26 2022/7/25
 **/
public class ModelHolder {

    public static void initModel() throws IOException, InstantiationException, IllegalAccessException,
            ClassNotFoundException {
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        org.springframework.core.io.Resource[] resources = resolver.getResources("classpath*:pri/damai/xiaowu" +
                "/anydoor/model/**/*.class");
        for (org.springframework.core.io.Resource resource : resources) {
            // 先获取resource的元信息，然后获取class元信息，最后得到 class 全路径
            String clsName = new SimpleMetadataReaderFactory().getMetadataReader(resource).getClassMetadata()
                    .getClassName();
            if (clsName.contains("base")) {
                continue;
            }
            Class<? extends BaseModel> aClass = (Class<? extends BaseModel>) Class.forName(clsName);
            BaseModel baseModel = aClass.newInstance();
            HandlerHolder.dataModelMap.put(baseModel.code(), aClass);
        }
    }

}
